package com.ftp;



//import org.junit.Test;


/**
 * 上传文件到MTP，然后从MTP将文件下载过来
 * 试验文件的上传于下载是否存在障碍
 * @author he.tianlin
 * @date 2017-08-09
 * ***/
public class UploadandDownloadFile {

	public static void main(String[] args){
	
		System.out.println("上传（1）下载（2）选择:"+ args[0]);
		if( "1".equals(args[0]) ){
			uploadFile(args);
		}
		else if( "2".equals(args[0]) ){
			downloadFile(args);
		}else {
			System.out.println("输入无效！上传（1）  下载（2），请重新选择 ");
		}
		
	}
	
	//上传文件
	public static void uploadFile(String[] args){
		
		System.out.println("FTP服务器hostname:" + args[1]);
		System.out.println("FTP服务器端口:" + args[2]);
		System.out.println("FTP登录账号:" + args[3]);
		System.out.println("FTP登录密码" + args[4]);
		System.out.println("FTP服务器保存目录:" + args[5]);
		System.out.println("上传到FTP服务器上的文件名:" + args[6]);
		System.out.println("本地文件路径:" + args[7]);
		
		String url = args[1];
		int port = Integer.parseInt(args[2]);
		String username = args[3];
		String password = args[4];
		String path = args[5];
		String desFilename = args[6];
		String srcFileName = args[7];
		try {
			SFTPUtil.upload(url, port, username, password, srcFileName, path, desFilename);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//Boolean flag = FtpUtil.uploadFile(url, port, username, password, path, desFilename, srcFileName);
//		if( flag ){
//			System.out.println("上传文件成功");
//		}else {
//			System.out.println("上传文件失败");
//		}
	}
	
	//下载文件
	public static void downloadFile(String[] args){
		
		System.out.println("FTP服务器hostname:" + args[1]);
		System.out.println("FTP服务器端口:" + args[2]);
		System.out.println("FTP登录账号:" + args[3]);
		System.out.println("FTP登录密码" + args[4]);
		System.out.println("FTP服务器上的相对路径:" + args[5]);
		System.out.println("要下载的文件名:" + args[6]);
		System.out.println("下载后保存到本地的路径:" + args[7]);
		
		String url = args[1];
		int port = Integer.parseInt(args[2]);
		String username = args[3];
		String password = args[4];
		String remotePath = args[5];
		String fileName = args[6];
		String localPath = args[7];
		String src = remotePath+fileName;
		try {
			SFTPUtil.download(url, port, username, password, src, localPath);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		Boolean flag = FtpUtil.downFile(url, port, username, password, remotePath, fileName, localPath);
//		if( flag ){
//			System.out.println("下载文件成功");
//		}else {
//			System.out.println("下载文件失败");
//		}
		
	}
	
	
	public void test(){
		
		String url = "192.168.30.134";
		int port = 21;
		String username = "center";
		String password = "123456";
		
//		String url = "192.168.30.191";
//		int port = 36;
//		String username = "tianlin";
//		String password = "a";
//		
		String[] args = { "1", url, port+"" , username , password,
				"htl/htl201708111008/" , "file20170814_1820" , "E:\\MTP.txt" };
		
		uploadFile(args);
		
		String[] args2 = { "2", url, port+"" , username , password,
				"htl/htl201708111008/" , "file20170814_1820" , "E://htl//we//"};
		downloadFile(args2);
		   
		
	}
	
	
}
